
using System;
using System.Text;
using LastFMGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using LastFMGenNHibernate.EN.LastFM;

namespace LastFMGenNHibernate.CAD.LastFM
{
        public partial class AlbumCAD : BasicCAD, IAlbumCAD
        {
        public AlbumCAD() : base ()
        {
        }

        public AlbumCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public AlbumEN ReadOIDDefault (int id)
        {
                AlbumEN albumEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        albumEN = (AlbumEN)session.Load (typeof(AlbumEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in AlbumCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return albumEN;
        }


        public int New_ (AlbumEN album)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (album.Grupo != null) {
                                album.Grupo = (LastFMGenNHibernate.EN.LastFM.GrupoEN)session.Load (typeof(LastFMGenNHibernate.EN.LastFM.GrupoEN), album.Grupo.Id);
                        }

                        session.Save (album);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in AlbumCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return album.Id;
        }

        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        AlbumEN albumEN = (AlbumEN)session.Load (typeof(AlbumEN),
                                id

                                );
                        session.Delete (albumEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in AlbumCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public void Modify (AlbumEN album)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        AlbumEN albumEN = (AlbumEN)session.Load (typeof(AlbumEN), album.Id);

                        albumEN.NombreAlbum = album.NombreAlbum;


                        albumEN.Etiqueta = album.Etiqueta;

                        session.Update (albumEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in AlbumCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        public System.Collections.Generic.IList<AlbumEN> DevolverAlbumes (int first, int size)
        {
                System.Collections.Generic.IList<AlbumEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(AlbumEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<AlbumEN>();
                        else
                                result = session.CreateCriteria (typeof(AlbumEN)).List<AlbumEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in AlbumCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        }
}
